package com.example.eureka_client.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.eureka_client.entity.CommentsAgree;
import com.example.eureka_client.entity.CommentsInfo;
import com.example.eureka_client.entity.CommentsReply;
import com.example.eureka_client.mapper.CommentsAgreeMapper;
import com.example.eureka_client.mapper.CommentsReplyMapper;
import com.example.eureka_client.service.ICommentsReplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 评论回复表 服务实现类
 * </p>
 *
 * @author plj
 * @since 2022-03-24
 */
@Service
public class CommentsReplyServiceImpl extends ServiceImpl<CommentsReplyMapper, CommentsReply> implements ICommentsReplyService {
    @Resource
    CommentsAgreeMapper commentsAgreeMapper;

    private final static SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public List<CommentsReply> getChildComments(String fatherCommentsId){
        return baseMapper.getChildComments(fatherCommentsId);
    }

    @Override
    public boolean addChildComment(CommentsReply commentsReply){
        Date nowTime = new Date();
        commentsReply.setId(UUID.randomUUID().toString());
        commentsReply.setCreateTime(sdf.format(nowTime));
        if(this.save(commentsReply)) return true;
        else return false;
    }

    @Override
    public boolean removeById(String id){
        return baseMapper.removeById(id);
    }

    @Override
    public boolean addChildAgree(CommentsAgree commentsAgree){
        // 更新赞同数
        CommentsReply commentsReply = baseMapper.getById(commentsAgree.getCommentsId());
        UpdateWrapper<CommentsReply> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ID", commentsAgree.getCommentsId());
        updateWrapper.set("LIKE_NUM", commentsReply.getLikeNum()+1);

        //添加关联
        if(commentsAgreeMapper.insert(commentsAgree) == 1 && this.update(null, updateWrapper)) return true;
        else return false;
    }

    @Override
    public boolean addChildNotAgree(CommentsAgree commentsAgree){
        // 更新踩数
        CommentsReply commentsReply = baseMapper.getById(commentsAgree.getCommentsId());
        UpdateWrapper<CommentsReply> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ID", commentsAgree.getCommentsId());
        updateWrapper.set("NOT_LIKE_NUM", commentsReply.getNotLikeNum()+1);

        //添加关联
        if(commentsAgreeMapper.insert(commentsAgree) == 1 && this.update(null, updateWrapper)) return true;
        else return false;
    }

    @Override
    public boolean reChildAgree(CommentsAgree commentsAgree){
        // 更新赞同数
        CommentsReply commentsReply = baseMapper.getById(commentsAgree.getCommentsId());
        UpdateWrapper<CommentsReply> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ID", commentsAgree.getCommentsId());
        updateWrapper.set("LIKE_NUM", commentsReply.getLikeNum()-1);

        //删除关联
        if(commentsAgreeMapper.deleteById(commentsAgree.getCommentsId(), commentsAgree.getUserId(), commentsAgree.getType()) && this.update(null, updateWrapper)) return true;
        else return false;
    }

    @Override
    public boolean reChildNotAgree(CommentsAgree commentsAgree){
        // 更新踩数
        CommentsReply commentsReply = baseMapper.getById(commentsAgree.getCommentsId());
        UpdateWrapper<CommentsReply> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ID", commentsAgree.getCommentsId());
        updateWrapper.set("NOT_LIKE_NUM", commentsReply.getNotLikeNum()-1);

        //删除关联
        if(commentsAgreeMapper.deleteById(commentsAgree.getCommentsId(), commentsAgree.getUserId(), commentsAgree.getType()) && this.update(null, updateWrapper)) return true;
        else return false;
    }

    @Override
    public boolean isChildAgree(CommentsAgree commentsAgree){
        CommentsAgree commentsAgree1 = commentsAgreeMapper.getByThree(commentsAgree.getCommentsId(), commentsAgree.getUserId(), commentsAgree.getType());
        if(commentsAgree1 != null) return true;
        else return false;
    }
}
